Puzzle 6 Explanation: Timeout

Understand how types can make differences.

We'll cover the following

Try it yourself#

Try executing the code below to see the result for yourself.

Using the timeout to understand more about types

Explanation#

When we write timeout := 3, the Go compiler will do type inference. In this case, it will infer that timeout is an int. Then in line 11, we multiply timeout (int type) with time.Millisecond (which is of time.Duration type), which is not allowed.

We have several options to fix this.

Change the type of timeout to time.Duration (line 9):

var timeout time.Duration = 3 

Use a type conversion to convert timeout to time.Duration (line 11):

time.Sleep(time.Duration(timeout) * time.Millisecond) 

Puzzle 6: Are We There Yet?

Puzzle 7: Can Numbers Lie?